﻿using Microsoft.Extensions.Logging;
using TcpJsonService.Core;

namespace TcpJsonService.Fvs
{
    /// <summary>
    /// 新控制器协议
    /// </summary>
    public class FvsControllerChannel_New : FvsControllerChannel
    {    
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="port">本地监听端口</param>
        /// <param name="logger">日志</param>
        public FvsControllerChannel_New(int port, ILogger logger)
            : base(port, logger, 0x99)
        {
        }
   
        protected override byte[] ToRealBuffer(byte[] buffer)
        {
            List<byte> temp = [.. buffer];
            temp[0] = 0x8A;
            temp.Insert(1, 0x99);
            temp.Insert(3, 0x00);
            if (temp[4]==0x11)
            {
                temp[3] = 0x7f;
            }
            return [.. temp];
        }

        protected override List<byte> ToCommonBuffer(List<byte> buffer)
        {
            List<byte> temp = [.. buffer];
            temp[0] = 0x90;
            temp.RemoveAt(1);
            temp.RemoveAt(2);

            if (temp[2] == 0x0b)
            {
                if (temp.Count < 8)
                {
                    _logger.LogInformation($"got controller 0b error {ByteConvert.ToHex(temp)}");
                }
                else
                {
                    /*
                     * 第6字节: if（byte10 & 0xf0 ==0） bit3=0;  else  bit3=1; 
                                if（byte10 == 0x04 || byte10 == 0x14）bit6=0;  else  bit6=1; 
                                其它位为0
                        第7字节: if（byte10 & 0xf0 ==0x20） bit3=1;  else  bit3=0; 
                                bit4=byte8的bit3 
                                其它位为0
                        第8字节: if（byte10 == 0x21） byte8=0x11;
                                else if（byte10 == 0x22） byte8=0x12;
                                else if（byte10 == 0x23） byte8=0x13;
                                else byte8=byte10;
                            
                     */
                    if (temp[3] == 0 && temp[4] == 0)
                    {
                        byte b6 = 0;
                        if ((temp[7] & 0xf0) == 0)
                        {
                            b6 |= 0x08;
                        }

                        if (temp[7] == 0x04 || temp[7] == 0x14)
                        {
                            b6 |= 0x40;
                        }
                        temp[5] = b6;

                        byte b7 = 0;
                        if ((temp[7] & 0xf0) == 0x20)
                        {
                            b7 |= 0x80;
                        }

                        if ((temp[5] & 0x08) == 1)
                        {
                            b7 |= 0x10;
                        }
                        temp[6] = b7;

                        byte b8;
                        if (temp[7] == 0x21)
                        {
                            b8 = 0x11;
                        }
                        else if (temp[7] == 0x22)
                        {
                            b8 = 0x12;
                        }
                        else if (temp[7] == 0x23)
                        {
                            b8 = 0x13;
                        }
                        else
                        {
                            b8 = temp[7];
                        }
                        temp[7] = b8;
                    }
                    else
                    {
                        _logger.LogInformation($"report 10 {ByteConvert.ToHex(temp)}");
                        List<byte> buffer10 =
                        [
                            0x90,
                            temp[1],
                            0x10,
                            0x01,
                            temp[4],
                            (temp[7] & 0x02) == 0 ? (byte)0 : (byte)1,
                            0xff,
                        ];
                        return buffer10;
                    }
                }
            }
            return temp;
        }
    }
}
